package com.cqc.evevationdrag01.view;

import android.content.Context;
import android.drm.DrmStore;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${cqc} on 2017/3/30.
 */

public class DragFrameLayout extends FrameLayout {

    private List<View> viewList = new ArrayList<>();;

    private ViewDragHelper dragHelper;

    public DragFrameLayout(@NonNull Context context) {
        super(context, null);
    }

    public DragFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public DragFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        viewList = new ArrayList<>();
        dragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
            /**
             * 当捕获到到事件的时候就把子view添加到集合中
             */
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                viewList.add(child);
                return false;
            }

            /**
             *  捕获到异常
             */
            @Override
            public void onViewCaptured(View capturedChild, int activePointerId) {
                super.onViewCaptured(capturedChild, activePointerId);
                if (mOnDragDropListener != null) {
                    mOnDragDropListener.onDragDrop(true);
                }
            }

            /**
             * 释放子View
             */
            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
                super.onViewReleased(releasedChild, xvel, yvel);
                if (mOnDragDropListener != null) {
                    mOnDragDropListener.onDragDrop(false);
                }
            }
        });
    }

    public void addDragView(View childView){
        viewList.add(childView);
    }

    /**
     * 让ViewDragHelper处理是否拦截事件
     */
//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        if(ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP){
//            dragHelper.cancel();
//            return  false;
//        }
//        return dragHelper.shouldInterceptTouchEvent(ev);
//    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getActionMasked();
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            dragHelper.cancel();
            return false;
        }
        return dragHelper.shouldInterceptTouchEvent(ev);
    }

    /**
     *  本身是否处理该事件
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        dragHelper.processTouchEvent(event);
        return true;
    }

    private OnDragDropListener mOnDragDropListener;

    public interface OnDragDropListener {
        void onDragDrop(boolean captured);
    }

    public void setOnDragDropListener(OnDragDropListener onDragDropListener) {
        this.mOnDragDropListener = onDragDropListener;
    }
}


